package com.boc.ewr.risk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boc.ewr.bcimp.entity.BusAcctInfo;
import com.boc.ewr.bcimp.service.IBusAcctInfoService;
import com.boc.ewr.business.entity.CaUnpriaccApply;
import com.boc.ewr.business.service.ICaUnpriaccApplyService;
import com.boc.ewr.risk.entity.CaUnpriaccUnusual;
import com.boc.ewr.risk.mapper.CaUnpriaccUnusualMapper;
import com.boc.ewr.risk.service.ICaUnpriaccUnusualService;
import com.boc.ewr.util.ToUserUtil;
import com.boc.ewr.util.file.enumeration.ProcessStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @Description: 特殊不对账账户异动表/异动特殊不对账账户清单
 * @Author: jeecg-boot
 * @Date: 2023-06-07
 * @Version: V1.0
 */
@Service
public class CaUnpriaccUnusualServiceImpl extends ServiceImpl<CaUnpriaccUnusualMapper, CaUnpriaccUnusual> implements ICaUnpriaccUnusualService {


    @Autowired
    private IBusAcctInfoService busAcctInfoService;

    @Autowired
    private ICaUnpriaccApplyService caUnpriaccApplyService;

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    /**
     * 导入特殊不对账账户明细后，与最新的对公账户信息中的“当月交易笔数”相匹配，
     * 如有交易不为“0”或者“空”，自动形成异动特殊不对账账户清单
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createInfo() {
        //清空当前记录

        //生成数据
        List<CaUnpriaccApply> caUnpriaccApplyList = caUnpriaccApplyService.list(
                new LambdaQueryWrapper<CaUnpriaccApply>()
                        .eq(CaUnpriaccApply::getCheckSta, ProcessStatusEnum.EFFECTIVE.getIndex())
        );
        if (caUnpriaccApplyList.size() == 0) {
            return false;
        }

        List<CaUnpriaccUnusual> caUnpriaccUnusualList = new ArrayList<>();
        for (CaUnpriaccApply caUnpriaccApply : caUnpriaccApplyList) {
            LambdaQueryWrapper<BusAcctInfo> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BusAcctInfo::getAccNo, caUnpriaccApply.getUnpriaccNo())
                    .isNotNull(BusAcctInfo::getMonFinaceTrans)
                    .or()
                    .notIn(BusAcctInfo::getMonFinaceTrans, "0");
            BusAcctInfo busAcctInfo = busAcctInfoService.getOne(wrapper);
            if (busAcctInfo == null) {
                continue;
            }

            CaUnpriaccUnusual caUnpriaccUnusual = new CaUnpriaccUnusual();
            BeanUtils.copyProperties(caUnpriaccApply, caUnpriaccUnusual);

            //重置Id
            caUnpriaccUnusual.setId(null);
            caUnpriaccUnusual.setTranCnt(busAcctInfo.getMonFinaceTrans());
            caUnpriaccUnusualList.add(caUnpriaccUnusual);
        }

        // 储存并推送
        this.sendMsg(caUnpriaccUnusualList);

        return this.saveOrUpdateBatch(caUnpriaccUnusualList);
    }

    private void sendMsg(Collection<CaUnpriaccUnusual> entityList) {

        // 获取推送用户
        Set<String> branchNoSet = new HashSet();
        for (CaUnpriaccUnusual caUnpriaccUnusual : entityList) {
            branchNoSet.add(caUnpriaccUnusual.getObranchNo());
        }
        String toUser = ToUserUtil.genToUser(branchNoSet);
        if (toUser != null) {
            MessageDTO messageDTO = new MessageDTO(
                    "system",
                    toUser,
                    "异动特殊不对账账户",
                    "异动特殊不对账账户清单已更新，请及时查看"
            );
            sysBaseAPI.sendSysAnnouncement(messageDTO);
        }

    }

}
